package com.cn.yang.dao.impl;

import com.cn.yang.dao.MainPageDao;
import com.cn.yang.model.contactUs.ContactUsModel;
import com.cn.yang.model.mainPage.HeadAndFootModel;
import com.cn.yang.model.mainPage.HeadLinkModel;
import com.cn.yang.model.mainPage.MainPageModel;
import com.cn.yang.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MainPageDaoImpl implements MainPageDao {
    JDBC jdbc = new JDBC();

    //头和尾的一些Model的查找和更新==============================
    @Override
    public HeadAndFootModel selectHeadFoot() {
        String sql = "select * from headandfoot";
        ResultSet resultSet = jdbc.getResultSet(sql);
        HeadAndFootModel model = new HeadAndFootModel();
        try{
            while (resultSet.next()){
                model.setId(resultSet.getInt("id"));
                model.setTitle(resultSet.getString("title"));
                model.setEnglishtitle(resultSet.getString("englishtitle"));
                model.setPhoto(resultSet.getString("photo"));
                model.setFoothref(resultSet.getString("foothref"));
                model.setFootwords(resultSet.getString("footwords"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int updataHeadFoot(HeadAndFootModel model) {
        String sql = "update headandfoot set `title`= '"+model.getTitle()+"'," +
                "`englishtitle`= '"+model.getEnglishtitle()+"'," +
                "`photo`= '"+model.getPhoto()+"'," +
                "`foothref`= '"+model.getFoothref()+"'," +
                "`footwords`= '"+model.getFootwords()+"'";
        int result = jdbc.update(sql);
        return result;
    }
//headLINK的 查找更新=======================================
    @Override
    public List<HeadLinkModel> selectHeadLink() {
        String sql = "select * from headlink";
        ResultSet resultSet = jdbc.getResultSet(sql);
        List<HeadLinkModel> list = new ArrayList<>();
        try{
            while (resultSet.next()){
                HeadLinkModel model = new HeadLinkModel();
                model.setId(resultSet.getInt("id"));
                model.setTitle(resultSet.getString("title"));
                model.setHref(resultSet.getString("href"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println(list);
        return list;
    }

    @Override
    public int addHeadLink(HeadLinkModel model) {
        String sql = "insert into headlink(`title`,`href`) values('"+model.getTitle()+"','"+model.getHref()+"')";
        int result = jdbc.update(sql);
        System.out.println(result);
        return result;
    }

    @Override
    public HeadLinkModel selectHeadLinkById(Integer id) {
        String sql = "select * from headlink where id = "+id;
        ResultSet resultSet = jdbc.getResultSet(sql);
        HeadLinkModel model = new HeadLinkModel();
        try{
            while (resultSet.next()){
                model.setId(resultSet.getInt("id"));
                model.setTitle(resultSet.getString("title"));
                model.setHref(resultSet.getString("href"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int updateById(HeadLinkModel model) {
        String sql = "update headlink set `title`= '"+model.getTitle()+"'," +
                "`title`= '"+model.getTitle()+"'," +
                "`href`= '"+model.getHref()+"' where id = '"+model.getId()+"'";
        int result = jdbc.update(sql);
        return result;
    }

    @Override
    public int deleteHeadLink(String id) {
        String sql = "delete from headlink where id = "+id;
        int result = jdbc.update(sql);
        return result;
    }
//中间首页的是否展示
    @Override
    public List<MainPageModel> findMainPage(MainPageModel model) {
        String sql = "select * from mainpage limit " + model.getPageUtil().getPageNumber() + "," + model.getPageUtil().getPageSize() + "";
        ResultSet resultSet = jdbc.getResultSet(sql);
        List<MainPageModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                MainPageModel message = new MainPageModel();
                message.setId(resultSet.getInt("id"));
                message.setTitle(resultSet.getString("title"));
                message.setLink(resultSet.getString("link"));
                message.setIsshow(resultSet.getString("isshow"));
                list.add(message);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        jdbc.closeConn();
        return list;
    }
    @Override
    public int countMainPage() {
        String sql = "select count(*) as count from mainpage ";
        ResultSet resultSet = jdbc.getResultSet(sql);
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        jdbc.closeConn();
        return count;
    }

    @Override
    public MainPageModel selectById(Integer id) {
        String sql = "select * from mainpage where id = "+id;
        ResultSet resultSet = jdbc.getResultSet(sql);
        MainPageModel model = new MainPageModel();
        try {
            while (resultSet.next()) {
                model.setId(resultSet.getInt("id"));
                model.setTitle(resultSet.getString("title"));
                model.setLink(resultSet.getString("link"));
                model.setIsshow(resultSet.getString("isshow"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        jdbc.closeConn();
        return model;
    }

    @Override
    public int updateMainPage(MainPageModel model) {
        String sql = "update mainpage set title = '"+model.getTitle()+"',link ='"+model.getLink()+"',isshow = '"+model.getIsshow()+"' where id = " +model.getId();
        int result = jdbc.update(sql);
        jdbc.closeConn();
        return result;
    }
}
